package com.lanchuan.soft.service.impl;

import com.lanchuan.soft.dao.IVersionDao;
import com.lanchuan.soft.entity.Version;
import com.lanchuan.soft.service.IVersionService;
import com.lanchuan.soft.utils.SystemConstant;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Log4j2
public class VersionService implements IVersionService {

    private IVersionDao versionDao;

    @Autowired
    public VersionService(IVersionDao versionDao) {
        this.versionDao = versionDao;
    }

    @Override
    public Map<String, Object> findLastVersionByType(int type) {

        Map<String, Object> resultMap = new HashMap<>();

        try {
            Version version = versionDao.findLastByType(type);

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("data", version);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("VersionService / findVersionByLast", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }
}
